import { ts,  rule, DslRule } from "qvog-dsl";

import { assert, beforeAll, describe, test } from 'vitest';
import { CHECK_MODE, testCaseCheck } from '../../common/testCommon';
import { CheckEntry, Rule } from '../../../../../src/Index';
import path from 'path';

let realPath: string = '';
let checkEntry: CheckEntry;

beforeAll(async () => {
    let dslRule: DslRule = rule<{ invalidPositions: ts.Node[], test: number }>()
        .match((node): node is ts.VariableDeclaration => ts.isVariableDeclaration(node))
        .when((node, { props }): boolean => {
            props.invalidPositions = [];
            props.test = 1;
            props.invalidPositions.push(node);
            return true;
        })
        .report((node, { api, props }) => {
            assert.equal(props.test, 1);
            
            props.invalidPositions.forEach((statement: ts.Node) => {
                api.report({
                    location: api.getLocation(statement),
                    severity: 'SEVERITY',
                    description: 'DESCRIPTION',
                    docPath: 'DOC_PATH',
                });
            });
        });
    const mockRule: Rule = new Rule('');
    checkEntry = await testCaseCheck('./test/unittest/sample/ArrayTypeCheckDSL', mockRule, CHECK_MODE.FILE2CHECK, dslRule, false, true);
    realPath = checkEntry.scene.getRealProjectDir();
})

describe('propsTest', () => {
    test('propsTest_001', () => {
        const detectFile: string = path.join(realPath, 'ArrayTypeCheckDSLReport.ts');
        const detectFileReport = checkEntry.fileChecks.find((fileCheck) => fileCheck.arkFile.getFilePath() === detectFile);
        assert.isDefined(detectFileReport, 'The file path is error.');
        assert.equal(detectFileReport?.issues.length, 2, 'The number of reported line is different from the expected number of line.');
    }),
        test('propsTest_002', () => {
            const detectFile: string = path.join(realPath, 'ArrayTypeCheckDSLNoReport.ts');
            const detectFileReport = checkEntry.fileChecks.find((fileCheck) => fileCheck.arkFile.getFilePath() === detectFile);
            assert.isDefined(detectFileReport, 'The file path is error.');
            assert.equal(detectFileReport?.issues.length, 3, 'The number of reported line is different from the expected number of line.');
        })
})